package comc.springbatch;


import comc.pojo.LlAppyInfo;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;

@Component
public class MyFileItemReader {

    public FlatFileItemReader flatFileItemReader() {
        FlatFileItemReader reader = new FlatFileItemReader();
        reader.setResource(new ClassPathResource("llappyinfo.txt"));
        reader.setLinesToSkip(1);//跳过第一行

//        解析数据
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"acceptNo", "batchNo", "rptorName"});

//        吧解析出来的数据进行映射为实体类
        DefaultLineMapper defaultLineMapper = new DefaultLineMapper();
        defaultLineMapper.setLineTokenizer(tokenizer);
        defaultLineMapper.setFieldSetMapper(new FieldSetMapper() {
            @Override
            public Object mapFieldSet(FieldSet fieldSet) throws BindException {
                LlAppyInfo llAppyInfo = new LlAppyInfo();
                llAppyInfo.setAcceptNo(fieldSet.readString("acceptNo"));
                llAppyInfo.setRptorName(fieldSet.readString("rptorName"));
                llAppyInfo.setBatchNo(fieldSet.readString("batchNo"));
                return llAppyInfo;
            }
        });
        defaultLineMapper.afterPropertiesSet();
        reader.setLineMapper(defaultLineMapper);
        return reader;
    }
}
